package cn.edu.lcu.cs.javaprogramming.thread.ticketoffice.thread;

import java.util.Random;

/**
 * 售票窗口
 *
 * @author ling
 * @date 2022/5/30 16:10
 */
public class TicketWindow extends Thread{
    private int remainingTicket;

    public TicketWindow(String name, int remainingTicket) {
        super(name);
        this.remainingTicket = remainingTicket;
    }

    @Override
    public void run() {
        while (remainingTicket > 0) {
            Random random = new Random();
            while (remainingTicket > 0) {
                System.out.println(Thread.currentThread().getName() + " 正在发售第 " + remainingTicket-- + " 张票");
                try {
                    Thread.sleep(random.nextInt(2000));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.err.println(Thread.currentThread().getName() + " 票已售罄！");
        }
    }
}
